package com.xukehong.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @Author: xkh
 * @Date: 2019/11/1 15:04
 * @Version 1.0
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    /**
     * redis失效key事件处理
     */
    @Override
    public void onMessage(Message message,byte[] pattern){
        //message.toString() 可以获取失效的Key
        String expiredKey = message.toString();
        log.info("失效key："+expiredKey);
        String service = StringUtils.substringBefore(expiredKey," ");
        if(service.equals("consumer")){
            String consumer = StringUtils.substringAfter(expiredKey," ");

        }
    }
}
